package cn.lix.sc.elasticsearch.dto;


import java.io.Serializable;

public class R<T> implements Serializable {

    private static final long serialVersionUID = -6867917087859108079L;

    public static final String MSG_OK = "OK";
    public static final String MSG_FAIL = "FAIL";
    public static final int FAIL = 500;
    public static final int OK = 200;

    private T data;
    private String msg;
    private int code;

    public static R success(Object data) {
        return new R(data, MSG_OK, OK);
    }

    public static R success() {
        return new R(null, MSG_OK, OK);
    }

    public static R fail(Throwable error) {
        return new R(null, error.getMessage(), FAIL);
    }

    public static R fail(String errorMsg) {
        return new R(null, errorMsg, FAIL);
    }

    public static R fail() {
        return new R(null, MSG_FAIL, FAIL);
    }

    public boolean isOk() {
        return this.code == OK;
    }

    private R(T data, String msg, int code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}
